<?php
/**
* @file $Id: Rollover.php 98 2006-08-08 23:53:54Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$years_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SYEAR FROM SCHOOL_YEARS WHERE SCHOOL_ID='".UserSchool()."'"),array(),array('SYEAR'));
$sems_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SYEAR FROM SCHOOL_SEMESTERS WHERE SCHOOL_ID='".UserSchool()."'"),array(),array('SYEAR'));
$qtrs_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SYEAR FROM SCHOOL_QUARTERS WHERE SCHOOL_ID='".UserSchool()."'"),array(),array('SYEAR'));
$pros_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,SYEAR FROM SCHOOL_PROGRESS_PERIODS WHERE SCHOOL_ID='".UserSchool()."'"),array(),array('SYEAR'));

$table_list = '<TABLE><TR><TD valign=top><B>'._('duplicate these marking periods:').'</B><BR>';
$table_list .= '<INPUT type=checkbox CHECKED DISABLED>'._('Year').'<BR><INPUT type=checkbox name=mps[SCHOOL_SEMESTERS] CHECKED>'._('Semesters').'<BR><INPUT type=checkbox name=mps[SCHOOL_QUARTERS] CHECKED>'._('Quarters').'<BR><INPUT type=checkbox name=mps[SCHOOL_PROGRESS_PERIODS]>'._('Progress Periods').'<BR>';
$table_list .= '</TD><TD width=30 valign=top><BR><BR><IMG SRC=assets/arrow_right.gif height=40></TD><TD valign=top><B>'._('to these school years:').'</B>';
for($i=UserSyear()-12;$i<UserSyear();$i++)
{
	if($years_RET[$i])
	{
		$table_list .= '<BR><INPUT type=checkbox CHECKED DISABLED><font color=gray>'.$i.((SystemPreferences('YEAR_TITLE')=='YYYY-YYYY')?'-'.($i+1):'');
		$table_list .= ' ('.count($years_RET[$i]).' '._('year').', '.count($sems_RET[$i]).' '._('semesters').', '.count($qtrs_RET[$i]).' '._('quarters').', '.count($pros_RET[$i]).' '._('progress periods').')</font>';
	}
	else
		$table_list .= '<BR><INPUT type=checkbox name=years['.$i.'] value=Y>'.$i.((SystemPreferences('YEAR_TITLE')=='YYYY-YYYY')?'-'.($i+1):'');
}
$table_list .= '</TD></TR></TABLE>';


DrawHeader(ProgramTitle());

if(Prompt(_('Confirm Rollover'),'',$table_list))
{
	foreach($_REQUEST['years'] as $syear=>$y)
	{
		DBQuery("UPDATE SCHOOL_YEARS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		DBQuery("INSERT INTO SCHOOL_YEARS (MARKING_PERIOD_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),'".$syear."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
	
		if($_REQUEST['mps']['SCHOOL_SEMESTERS'])
		{
			DBQuery("UPDATE SCHOOL_SEMESTERS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			DBQuery("INSERT INTO SCHOOL_SEMESTERS (MARKING_PERIOD_ID,YEAR_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS y WHERE y.SYEAR='".$syear."' AND y.ROLLOVER_ID=s.YEAR_ID),'".$syear."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_SEMESTERS s WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		
			if($_REQUEST['mps']['SCHOOL_QUARTERS'])
			{
				DBQuery("UPDATE SCHOOL_QUARTERS SET ROLLOVER_ID=MARKING_PERIOD_ID WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
				DBQuery("INSERT INTO SCHOOL_QUARTERS (MARKING_PERIOD_ID,SEMESTER_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS s WHERE s.SYEAR='".$syear."' AND s.ROLLOVER_ID=q.SEMESTER_ID),'".$syear."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_QUARTERS q WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		
				if($_REQUEST['mps']['SCHOOL_PROGRESS_PERIODS'])
					DBQuery("INSERT INTO SCHOOL_PROGRESS_PERIODS (MARKING_PERIOD_ID,QUARTER_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_QUARTERS q WHERE q.SYEAR='".$syear."' AND q.ROLLOVER_ID=p.QUARTER_ID),'".$syear."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_PROGRESS_PERIODS p WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
			}
		}
	}
	echo '<FORM action=Modules.php?modname='.$_REQUEST['modname'].' method=POST>';
	DrawHeader('<IMG SRC=assets/check.gif>'._('The data have been rolled.'),'<INPUT type=submit value='._('OK').'>');
	echo '</FORM>';
	unset($_SESSION['_REQUEST_vars']['tables']);
	unset($_SESSION['_REQUEST_vars']['delete_ok']);
}


?>